//index.js
import {handleRelation} from '../../../../utils/tool'
var app = getApp()
Page({
  data: {
    canDel: false,
    guardian: null //当前监护人详情
  },
  onLoad(options){
    var canDel = this.data.canDel
    var guardian = JSON.parse(options.guardian)
    if (options.adminId == app.userInfo.userId || app.userInfo.userId == guardian.guardianUserId) {
      canDel = true
    } else {
      canDel = false
    }
    this.setData({
      canDel,
      adminId: options.adminId,
      guardian: handleRelation(guardian)
    })
  },
  delRelation(){
    var _this = this
    wx.showModal({
      title: '提示',
      content: '确定删除监护人吗？',
      success(res){
        if(res.confirm) {
          _this.removeRelation()
        }
      }
    })
  },
  // 请求服务解除关系
  removeRelation(){
    var form = {
      guardianUserId: this.data.guardian.guardianUserId,
      childUserId: this.data.guardian.childUserId,
      adminUserId: this.data.adminId
    }
    var _this = this
    app.middleware.requestAjax({
      serAddr: app.serAddrs['delRelation'],
      data: form,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success() {
        wx.showToast({
          title: '删除关系成功',
          icon: 'none',
          success(){
            _this.updatePrevPageData()
            setTimeout(() => {
              wx.navigateBack({
                delta: 3
              })
            },1000)
          }
        })
      },
      fail(err){
        wx.showToast({
          title: err.msg,
          icon: 'none'
        })
      }
    })
  },
  // 更新上一页面的数据
  updatePrevPageData(){
    var pages = getCurrentPages()
    var prevPage = pages[pages.length - 4]
    prevPage.setData({
      onShowRefresh: true
    })
  }
})
